package anal;

import libraryRun.MainFrame;
import mySQLServer.Link;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Scanner;

public class ShowTable extends JFrame {
    private int sum;
    private ShowTable myAnaly;
    public ShowTable(){
        super("图书分析");
        myAnaly=this;
    }

    public void showTable (MainFrame myJFrame){
        setLayout(null);
        Container container=this.getContentPane();
        setSize(950,600);

        //面板
        JPanel p1=new JPanel(new FlowLayout(FlowLayout.CENTER,0,35));
        JPanel p2=new JPanel(new FlowLayout(FlowLayout.CENTER,10,35));
        JPanel p3=new JPanel(new BorderLayout());

        //标签
        JLabel label1=new JLabel("书 籍 分 析");

        //按钮
        JButton button1=new JButton("查 询");
        JButton button2=new JButton("取 消");

        //表格
        String[] tableHeader={"书籍Id","书籍名","借阅次数"};
        Object[][] tableData=new Object[50][3];
        JTable jTable=new JTable(tableData,tableHeader);

        //滚动框
        JScrollPane jScrollPane=new JScrollPane(jTable);

        //颜色
        Color color1=new Color(203, 26, 40);
        Color color2=new Color(87, 78, 64, 116);
        Color color3=new Color(7, 124, 161, 255);
        Color color4=new Color(13, 50, 197,225);

        //字体
        Font font1=new Font("宋体",Font.BOLD,45);
        Font font2=new Font("宋体",Font.BOLD,30);
        Font font3=new Font("宋体",Font.BOLD,17);
        Font font4=new Font("宋体",Font.BOLD,15);
        Font font5=new Font("宋体",Font.BOLD,14);

        //设置字体
        label1.setFont(font1);

        //设置大小
        button1.setPreferredSize(new Dimension(100,30));
        button2.setPreferredSize(new Dimension(100,30));

        //设置位置
        p1.setBounds(0,10,950,130);
        p2.setBounds(330,400,250,100);
        p3.setBounds(260,150,430,220);

        //表头设置
        jTable.getTableHeader().setResizingAllowed(false); //用户不可以通过拖拉表头间调整各列大小
        jTable.getTableHeader().setReorderingAllowed(false); //用户不可以通过拖动表头以重新排列各列
        jTable.getTableHeader().setFont(font4);
        jTable.getTableHeader().setForeground(color1);

        //表格设置
        jTable.setRowHeight(20); //设置行高
        jTable.setFont(font5);
        jTable.setForeground(color4);
        jTable.setSelectionForeground(color3); //设置被选中的行前景(字体颜色)
        jTable.setSelectionBackground(color2); //设置被选中的行背景


        //放入面板
        p1.add(label1);

        p2.add(button1);
        p2.add(button2);

        p3.add(jScrollPane);

        //放入容器
        container.add(p1);
        container.add(p2);
        container.add(p3);

        //监听器
        ImageIcon imageIcon1=new ImageIcon("icon/imageIcon6.jpg");
        imageIcon1.setImage(imageIcon1.getImage().getScaledInstance(50,45,Image.SCALE_AREA_AVERAGING));
        button1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                Link link=new Link();
                try{
                    Connection con=link.linkDatebase();
                    String sql="select bookId,bookName,count(*) 借阅次数  from [book_user] group by bookId,bookName";
                    searchJTable(con,jTable,sql);
                    JOptionPane.showMessageDialog(myAnaly,"分析成功","借阅分析",1,imageIcon1);

                    link.closeDatebase(con);
                }catch (ClassNotFoundException e1){
                    e1.printStackTrace();
                }catch (SQLException exception){
                    exception.printStackTrace();
                }
            }
        });
        button2.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                myJFrame.setVisible(true);
                myAnaly.dispose();
            }
        });
        addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                myJFrame.setVisible(true);
            }
        });

        setIconImage((new ImageIcon("icon/imageIcon5.jpg")).getImage());
        setDefaultCloseOperation(DISPOSE_ON_CLOSE);
        setLocationRelativeTo(null);
        setResizable(false);
        setVisible(true);
    }

    public void searchJTable (Connection con, JTable jTable, String sql) throws SQLException {
        sum=0;
        Statement stmt=con.createStatement();
        ResultSet rs=stmt.executeQuery(sql);
        while (rs.next()){
            jTable.getModel().setValueAt(rs.getString("bookId"),sum,0);
            jTable.getModel().setValueAt(rs.getString("bookName"),sum,1);
            jTable.getModel().setValueAt(rs.getString("借阅次数"),sum,2);
            sum++;
        }

    }

}
